package com.itheima.ai.tools;


import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.itheima.ai.entity.po.Course;
import com.itheima.ai.entity.po.CourseReservation;
import com.itheima.ai.entity.po.School;
import com.itheima.ai.entity.query.CourseQuery;
import com.itheima.ai.service.ICourseReservationService;
import com.itheima.ai.service.ICourseService;
import com.itheima.ai.service.ISchoolService;
import lombok.RequiredArgsConstructor;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@RequiredArgsConstructor
public class courseTools {

    private final ICourseService courseService;
    private final ISchoolService schoolService;
    private final ICourseReservationService  courseReservationService;

    @Tool(description = "根据条件查询课程")
    public List<Course> queryCourse(@ToolParam(description = "查询的条件")CourseQuery courseQuery) {
        if (courseQuery == null) {
            return courseService.list();
        }
        QueryChainWrapper<Course> wrapper = courseService.query().eq(courseQuery.getType() != null, "type", courseQuery.getType())
                .le(courseQuery.getEdu() != null, "edu", courseQuery.getEdu());
        if (courseQuery.getSorts() != null && !courseQuery.getSorts().isEmpty()) {
            for (CourseQuery.Sort sort : courseQuery.getSorts()) {
                wrapper.orderBy(true,sort.getAsc(),sort.getField());
            }
        }
        return wrapper.list();
    }

    @Tool(description = "查询学校")
    public List<School> querySchool() {
        return schoolService.list();
    }

    @Tool(description = "保存预约课程，返回预约单号")
    public Integer saveReservation(@ToolParam(description = "预约课程") String course,
                                   @ToolParam(description = "学生姓名") String studentName,
                                    @ToolParam(description = "联系方式") String contactInfo,
                                    @ToolParam(description = "预约校区") String school,
                                    @ToolParam(description = "备注",required = false) String remark
                                   ) {
        CourseReservation cr = new CourseReservation();
        cr.setCourse(course);
        cr.setStudentName(studentName);
        cr.setContactInfo(contactInfo);
        cr.setSchool(school);
        cr.setRemark(remark);
        courseReservationService.save(cr);
        return cr.getId();

    }
}
